package com.bw.service.impl;

import com.bw.entity.TbProject;
import com.bw.entity.TbUser;
import com.bw.mapper.ProjectMapper;
import com.bw.service.ProjectService;
import com.bw.vo.CallAndInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImp implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;


    @Override
    public List getprojectList(String mohu) {
        return projectMapper.getprojectList(mohu);
    }

    @Override
    public void qi(String id) {
        this.projectMapper.qi(id);
    }

    @Override
    public void ting(String id) {
        this.projectMapper.ting(id);
    }

    @Override
    public void delete(String id) {
        this.projectMapper.delete(id);
    }

    @Override
    public List getuserlist() {
        return this.projectMapper.getuserlist();
    }

    @Override
    public List getinsertlist(String mohu) {
        return this.projectMapper.getinsertlist(mohu);
    }

    @Override
    public void addProject(TbProject project) {
        String typeid = project.getInsertIds();
        if(typeid.contains(",") && typeid!=null){
            String[] split = typeid.split(",");
            String typename = "";
            for(String tid : split){
                //根据接口id查询
                String tname = this.projectMapper.findByTid(tid);
                typename +=","+tname;
            }
            typename = typename.substring(1);
            project.setTypeid(typename);
            System.out.println("接口名称"+typename);
        }else{
            String tname = this.projectMapper.findByTid(typeid);
            project.setTypeid(tname);
        }

        String userIds = project.getUserIds();
        if(userIds.contains(",") && userIds!=null){
            String[] split1 = userIds.split(",");
            String usernames = "";
            for(String uid : split1){
                //根据用户id查询u
                String uname = this.projectMapper.findByuid(uid);
                usernames +=","+uname;
            }
            usernames = usernames.substring(1);
            project.setUsersid(usernames);
        }else{
            String uname = this.projectMapper.findByuid(userIds);
            project.setUsersid(uname);
        }
        this.projectMapper.addProject(project);
    }

    @Override
    public void userfp(String ids,int id) {
        //根据ID查询项目数据
        TbProject project = this.projectMapper.findByPid(id);
        String usernames = "";
        String[] split = ids.split(",");
        for (String uid : split){
            String uname = this.projectMapper.findByuid(uid);
            usernames +=","+uname;
        }
        usernames = usernames.substring(1);
        project.setUsersid(usernames);
        //修改用户配置
        this.projectMapper.updateuserid(project);
        System.out.println("用户配置"+project);
    }

    @Override
    public void interfp(String ids, int id) {
        TbProject project = this.projectMapper.findByPid(id);
        String[] split = ids.split(",");
        String typename = "";
        for(String tid : split){
            //根据接口id查询
            String tname = this.projectMapper.findByTid(tid);
            typename +=","+tname;
        }
        typename = typename.substring(1);
        project.setTypeid(typename);
        this.projectMapper.updateTypeid(project);
    }

    @Override
    public List calllist(String tid) {
        List<CallAndInter> list = this.projectMapper.calllist(tid);

        for(CallAndInter s : list){
            String proid = s.getProid();
            System.out.println("proid==="+proid);
            if(proid.contains(",") && proid!=null){
                String[] split = proid.split(",");
                String pidAll = "";
                for(String pid : split){
                    String byProid = this.projectMapper.findByProid(pid);
                    pidAll+=","+byProid;
                }
                pidAll = pidAll.substring(1);
                s.setProidAll(pidAll);
            }else{
                String pidAll1 = this.projectMapper.findByProid(proid);
                s.setProidAll(pidAll1);
            }
            System.out.println("proidAll==="+s.getProidAll());
        }
//        for(CallAndInter s : list){
//             String proidAll = s.getProidAll();
//             if(proidAll.contains(",") && proidAll!=null){
//                 String[] split = proidAll.split(",");
//                 int length = split.length;
//                 s.setProjectnum(length);
//             }
//        }
        return  list;
    }
}
